const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    inputValue:'',
    queryParams:{
     1:{
       pageNumber:1,
       pageSize:10,
       lastPage:false
     },
     2:{
       pageNumber:1,
       pageSize:10,
       lastPage:false
     }
    },
    page:1,
    pageSize:99,
    isEnd:false,
    videolistdata:[],
    articlelistdata:[],
    recipelistdata:[],
    categoryId:'',
    source:3,
    type:2,
    isloading:true,
  },
  onLoad: function (options) {
    
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow(){
    this.setData({
      page:1,
      pageSize:99,
      isEnd:false,
      videolistdata:[],
      articlelistdata:[],
      recipelistdata:[],
    })
    this.hotRecommend(1)
    this.hotRecommend(2)
  },
  change(e){
    this.setData({
      type:e.currentTarget.dataset.index,
    })
  },
  hotRecommend(source){
   
    // Controller.hotRecommend().then(res=>{
    //   if(res.data.baseData ){
    //     this.setData({
    //       videolistdata:res.data.baseData.video,
    //       articlelistdata:res.data.baseData.article,
    //       recipelistdata:res.data.baseData.recipe,
    //     })
    //   }
    //   this.setData({
    //     isloading:false
    //   })
    // })
    let indexSource=source||this.data.type
    let pageSize=this.data.queryParams[indexSource].pageSize
    let pageNumber=this.data.queryParams[indexSource].pageNumber

    Controller.hotRecommendV2({pageSize,pageNumber,indexSource}).then(res=>{
      
      if(indexSource==1){
        
        this.setData({
          articlelistdata:this.data.articlelistdata.concat(res.data.baseData.list)
        })
      
        
      
      }
      if(indexSource==2){
        this.setData({
          videolistdata:this.data.videolistdata.concat(res.data.baseData.list)
        })
       
      }
    
      this.data.queryParams[indexSource].lastPage=res.data.baseData.lastPage;

      console.log('articlelistdata',this.data.articlelistdata)

    }).catch(err=>{
      console.log(err)
    })
    
  },
  golink1(e){
    var item=e.currentTarget.dataset.item;
    if(item.lessonId){
      wx.navigateTo({
        url:"/pages/videoDetails/videoDetails?id="+item.id
      });
    }else{
      wx.navigateTo({
        url:"/pages/news/detail?id="+item.id
      });
    }
  },
  golink2(e){
    var id=e.currentTarget.dataset.id;
    var type=e.currentTarget.dataset.type;
    wx.navigateTo({
      url:"/pages/recipesDetails/recipesDetails?id="+id
    });
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onReachBottom(){
    let lastPage=this.data.queryParams[this.data.type].lastPage;
    if(!lastPage){
     return wx.showToast({
       title: '没有更多数据了',
       icon:'none'
     })
    }
    this.data.queryParams[this.data.type].pageNumber++
  }
})
